﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace FaWorksSolution.MvcApplication.Controllers
{
    public partial class ManageController : Controller
    {
        #region Data
        public ActionResult ExportData()
        {
            try
            {
                var serverPath = Server.MapPath("~/temp/data/");
                if (!Directory.Exists(serverPath))
                {
                    Directory.CreateDirectory(serverPath);
                }

                //导出前备份
                System.IO.FileInfo fi;
                fi = new System.IO.FileInfo(serverPath + "Group.json");
                if (fi.Exists) {
                    fi.CopyTo(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length)
                        + fi.LastWriteTime.ToString("yyyyMMddHHmmss")
                        + fi.Extension, true);
                }
                fi = new System.IO.FileInfo(serverPath + "Domain.json");
                if (fi.Exists)
                {
                    fi.CopyTo(fi.FullName.Substring(0, fi.FullName.Length - fi.Extension.Length)
                        + fi.LastWriteTime.ToString("yyyyMMddHHmmss")
                        + fi.Extension, true);
                }

                StringBuilder sbSql;

                sbSql = new StringBuilder();
                foreach (var group in db.AssetsGroups.Include("Files.Cdns"))
                {
                    sbSql.AppendLine(JsonConvert.SerializeObject(group));
                }
                using (TextWriter tw = new StreamWriter(serverPath + "Group.json", false))
                {
                    tw.WriteLine(sbSql.ToString());
                }

                sbSql = new StringBuilder();
                foreach (var domain in db.SitemapDomains.Include("Pages.Pages.Pages.Pages.Pages.Pages.Pages.Pages.Pages.Pages"))
                {
                    sbSql.AppendLine(JsonConvert.SerializeObject(domain));
                }
                using (TextWriter tw = new StreamWriter(serverPath + "Domain.json", false))
                {
                    tw.WriteLine(sbSql.ToString());
                }

                ViewBag.Message = "数据导出成功！";
            }
            catch (Exception ex) {
                ViewBag.Message = "数据导出失败：" + ex.Message;
            }
            return View("Data");
        }

        public ActionResult ImportData()
        {
            try
            {
                var serverPath = Server.MapPath("~/temp/data/");
                if (!Directory.Exists(serverPath))
                {
                    throw new FileNotFoundException("没有导出的数据文件！");
                }

                using (TextReader tr = new StreamReader(serverPath + "Group.json"))
                {
                    while(true){
                        var str = tr.ReadLine();
                        if (string.IsNullOrEmpty(str)) {
                            break;
                        }

                        var group = JsonConvert.DeserializeObject<Models.Assets.Group>(str);
                        db.AssetsGroups.Add(group);
                    }
                }

                using (TextReader tr = new StreamReader(serverPath + "Domain.json", false))
                {
                    while (true)
                    {
                        var str = tr.ReadLine();
                        if (string.IsNullOrEmpty(str))
                        {
                            break;
                        }

                        var domain = JsonConvert.DeserializeObject<Models.Sitemap.Domain>(str);
                        db.SitemapDomains.Add(domain);
                    }
                }

                db.SaveChanges();

                ViewBag.Message = "数据导入成功！";
            }
            catch (Exception ex)
            {
                ViewBag.Message = "数据导入失败：" + ex.Message;
            }
            return View("Data");
        }
        #endregion
    }
}
